package eraikikon2;

//import java.awt.BorderLayout;
//import javax.swing.JPanel;
//import javax.swing.JFrame;
//import java.awt.Dimension;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Rectangle;
import java.util.Vector;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;


public class GUI extends JFrame {

	private static final long serialVersionUID = 1L;

	private JPanel jContentPane = null;

	private JPanel jPanel = null;

	private JPanel jPanel1 = null;

	private JButton jButton1 = null;

	private JButton jButton2 = null;

	private JButton jButton = null;

	private JButton jButton3 = null;

	private JButton jButton4 = null;

	private JButton jButton5 = null;

	private JButton jButton6 = null;

	private JLabel jLabel = null;

	private JLabel jLabel1 = null;

	private JTextField jTextField = null;

	private JPasswordField jPasswordField = null;

	private GUI gui;

	private Kudeatzailea kudeatzailea = new Kudeatzailea(); // @jve:decl-index=0:

	private Vector<TxartelIrakurgailua> nora;

	private Integer unekoGunea;

	/**
	 * This is the default constructor
	 */
	public GUI() {
		super();
		initialize();
		gui = this;
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(798, 554);
		this.setContentPane(getJContentPane());
		this.setTitle("JFrame");
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(getJPanel(), BorderLayout.NORTH);
			jContentPane.add(getJPanel1(), BorderLayout.CENTER);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanel() {
		if (jPanel == null) {
			jLabel1 = new JLabel();
			jLabel1.setBounds(new Rectangle(30, 75, 196, 31));
			jLabel1.setText("Pasahitza sartu:");
			jLabel = new JLabel();
			jLabel.setBounds(new Rectangle(30, 30, 196, 31));
			jLabel.setText("Txartel identifikadorea sartu:");
			jPanel = new JPanel();
			jPanel.setLayout(null);
			jPanel.setPreferredSize(new Dimension(50, 125));
			jPanel.add(getJButton(), null);
			jPanel.add(jLabel, null);
			jPanel.add(jLabel1, null);
			jPanel.add(getJTextField(), null);
			jPanel.add(getJPasswordField(), null);
		}
		return jPanel;
	}

	/**
	 * This method initializes jPanel1
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanel1() {
		if (jPanel1 == null) {
			GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
			gridBagConstraints5.gridx = 2;
			gridBagConstraints5.gridy = 1;
			GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
			gridBagConstraints4.gridx = 1;
			gridBagConstraints4.gridy = 1;
			GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
			gridBagConstraints3.gridx = 0;
			gridBagConstraints3.gridy = 1;
			GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
			gridBagConstraints2.gridx = 2;
			gridBagConstraints2.gridy = 0;
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.gridx = 1;
			gridBagConstraints1.gridy = 0;
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.gridx = 0;
			gridBagConstraints.gridy = 0;
			jPanel1 = new JPanel();
			jPanel1.setLayout(new GridBagLayout());
			jPanel1.add(getJButton1(), gridBagConstraints);
			jPanel1.add(getJButton2(), gridBagConstraints1);
			jPanel1.add(getJButton3(), gridBagConstraints2);
			jPanel1.add(getJButton4(), gridBagConstraints3);
			jPanel1.add(getJButton5(), gridBagConstraints4);
			jPanel1.add(getJButton6(), gridBagConstraints5);
		}
		return jPanel1;
	}

	/**
	 * This method initializes jButton1
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJButton1() {
		if (jButton1 == null) {
			jButton1 = new JButton();
			jButton1.setIcon(new ImageIcon(getClass().getResource(
					"/EI_EI/g1.png")));
			jButton1.setVisible(false);
			jButton1.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.out.println("actionPerformed()"); 
					for (TxartelIrakurgailua elem : nora)
						if ((elem.getHelburuGunea() == 1)
								&& (unekoGunea == elem.getGunea()))
						{
							kudeatzailea.bidaliTxartelIrakurgailuId(elem
									.getId());
							kudeatzailea.bidaliAtea(elem.getAtea());
							System.out.print("segui:" + elem.getAtea());
						}
					kudeatzailea.bidaliHelburuGunea(1);
					kudeatzailea.sarbideaEskatu(Integer.parseInt(jTextField
							.getText()), jPasswordField.getText());
					gui.botoiakEguneratu();
				}
			});
		}
		return jButton1;
	}

	/**
	 * This method initializes jButton2
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJButton2() {
		if (jButton2 == null) {
			jButton2 = new JButton();
			jButton2.setIcon(new ImageIcon(getClass().getResource(
					"/EI_EI/g2.png")));
			jButton2.setVisible(false);
			jButton2.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.out.println("actionPerformed()");
					for (TxartelIrakurgailua elem : nora) {
						if ((elem.getHelburuGunea() == 2)
								&& (unekoGunea == elem.getGunea()))
						{
							kudeatzailea.bidaliTxartelIrakurgailuId(elem
									.getId());
							kudeatzailea.bidaliAtea(elem.getAtea());
						}
					}
					kudeatzailea.bidaliHelburuGunea(2);
					kudeatzailea.sarbideaEskatu(Integer.parseInt(jTextField
							.getText()), jPasswordField.getText());
					gui.botoiakEguneratu();
				}
			});
		}
		return jButton2;
	}

	/**
	 * This method initializes jButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton();
			jButton.setBounds(new Rectangle(570, 45, 151, 31));
			jButton.setText("Eraikinean sartu");
			jButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.out.println("actionPerformed()"); 
//					System.out.print(kudeatzailea.bidaliErabiltzailea().getPasahitza());
//					if (kudeatzailea.bidaliErabiltzailea().getPasahitza()==jPasswordField.getText())
					gui.botoiakEguneratu();
//					else
//					JOptionPane.showMessageDialog(null, "pasahitza gaizki");
					
				}
			});
		}
		return jButton;
	}

	/**
	 * This method initializes jButton3
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJButton3() {
		if (jButton3 == null) {
			jButton3 = new JButton();
			jButton3.setIcon(new ImageIcon(getClass().getResource(
					"/EI_EI/g3.png")));
			jButton3.setVisible(false);
			jButton3.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.out.println("actionPerformed()");
					for (TxartelIrakurgailua elem : nora) {
						if ((elem.getHelburuGunea() == 3)
								&& (unekoGunea == elem.getGunea()))
						{
							kudeatzailea.bidaliTxartelIrakurgailuId(elem
									.getId());
							kudeatzailea.bidaliAtea(elem.getAtea());
						}
					}
					kudeatzailea.bidaliHelburuGunea(3);
					kudeatzailea.sarbideaEskatu(Integer.parseInt(jTextField
							.getText()), jPasswordField.getText());
					gui.botoiakEguneratu();
				}
			});
		}
		return jButton3;
	}

	/**
	 * This method initializes jButton4
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJButton4() {
		if (jButton4 == null) {
			jButton4 = new JButton();
			jButton4.setIcon(new ImageIcon(getClass().getResource(
					"/EI_EI/g4.png")));
			jButton4.setVisible(false);
			jButton4.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.out.println("actionPerformed()");
					for (TxartelIrakurgailua elem : nora) {
						if ((elem.getHelburuGunea() == 4)
								&& (unekoGunea == elem.getGunea()))
						{
							kudeatzailea.bidaliTxartelIrakurgailuId(elem
									.getId());
							kudeatzailea.bidaliAtea(elem.getAtea());
						}
					}
					kudeatzailea.bidaliHelburuGunea(4);
					kudeatzailea.sarbideaEskatu(Integer.parseInt(jTextField
							.getText()), jPasswordField.getText());
					gui.botoiakEguneratu();
				}
			});
		}
		return jButton4;
	}

	/**
	 * This method initializes jButton5
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJButton5() {
		if (jButton5 == null) {
			jButton5 = new JButton();
			jButton5.setIcon(new ImageIcon(getClass().getResource(
					"/EI_EI/g5.png")));
			jButton5.setVisible(false);
			jButton5.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.out.println("actionPerformed()");
					for (TxartelIrakurgailua elem : nora) {
						if ((elem.getHelburuGunea() == 5)
								&& (unekoGunea == elem.getGunea()))
						{
							kudeatzailea.bidaliTxartelIrakurgailuId(elem
									.getId());
							kudeatzailea.bidaliAtea(elem.getAtea());
						}
					}
					kudeatzailea.bidaliHelburuGunea(5);
					kudeatzailea.sarbideaEskatu(Integer.parseInt(jTextField
							.getText()), jPasswordField.getText());
					gui.botoiakEguneratu();
				}
			});
		}
		return jButton5;
	}

	/**
	 * This method initializes jButton6
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJButton6() {
		if (jButton6 == null) {
			jButton6 = new JButton();
			jButton6.setIcon(new ImageIcon(getClass().getResource(
					"/EI_EI/g6.png")));
			jButton6.setVisible(false);
			jButton6.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.out.println("actionPerformed()");
					for (TxartelIrakurgailua elem : nora) {
						System.out.println("unekoGunea get:" + elem.getGunea());
						System.out.println("helburugunea:" + elem.getGunea());
						System.out.println("unekoGunea:" + unekoGunea);
						if ((elem.getHelburuGunea() == 6)
								&& (unekoGunea == elem.getGunea()))
						{
							kudeatzailea.bidaliTxartelIrakurgailuId(elem
									.getId());
							kudeatzailea.bidaliAtea(elem.getAtea());
						}
					}
					kudeatzailea.bidaliHelburuGunea(6);
					kudeatzailea.sarbideaEskatu(Integer.parseInt(jTextField
							.getText()), jPasswordField.getText());
					gui.botoiakEguneratu();
				}
			});
		}

		return jButton6;
	}

	/**
	 * This method initializes jTextField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getJTextField() {
		if (jTextField == null) {
			jTextField = new JTextField();
			jTextField.setBounds(new Rectangle(300, 30, 151, 31));
		}
		return jTextField;
	}

	/**
	 * This method initializes jPasswordField
	 * 
	 * @return javax.swing.JPasswordField
	 */
	private JPasswordField getJPasswordField() {
		if (jPasswordField == null) {
			jPasswordField = new JPasswordField();
			jPasswordField.setBounds(new Rectangle(300, 75, 151, 31));
		}
		return jPasswordField;
	}

	public void botoiakEguneratu() {
		jButton1.setVisible(false);
		jButton2.setVisible(false);
		jButton1.setBackground(Color.lightGray);
		jButton2.setBackground(Color.lightGray);
		jButton3.setBackground(Color.lightGray);
		jButton4.setBackground(Color.lightGray);
		jButton5.setBackground(Color.lightGray);
		jButton6.setBackground(Color.lightGray);
		jButton3.setVisible(false);
		jButton4.setVisible(false);
		jButton5.setVisible(false);
		jButton6.setVisible(false);
		Itis kokagunea = kudeatzailea.getUnekoKokagunea(Integer
				.parseInt(jTextField.getText()));
		nora = kudeatzailea.noraJoan(kokagunea.getGunea());

		unekoGunea = kokagunea.getGunea();
		if (unekoGunea == 1) {
			jButton1.setBackground(Color.orange);
			jButton1.setVisible(true);
		}
		if (unekoGunea == 2) {
			jButton2.setBackground(Color.orange);
			jButton2.setVisible(true);
		}
		if (unekoGunea == 3) {
			jButton3.setBackground(Color.orange);
			jButton3.setVisible(true);
		}
		if (unekoGunea == 4) {
			jButton4.setBackground(Color.orange);
			jButton4.setVisible(true);
		}
		if (unekoGunea == 5) {
			jButton5.setBackground(Color.orange);
			jButton5.setVisible(true);
		}
		if (unekoGunea == 6) {
			jButton6.setBackground(Color.orange);
			jButton6.setVisible(true);
		}
		int i = 0;
		while (i < nora.size()) {
			Integer gune = nora.elementAt(i).getHelburuGunea();
			if (gune == 1)
				jButton1.setVisible(true);
			if (gune == 2)
				jButton2.setVisible(true);
			if (gune == 3)
				jButton3.setVisible(true);
			if (gune == 4)
				jButton4.setVisible(true);
			if (gune == 5)
				jButton5.setVisible(true);
			if (gune == 6)
				jButton6.setVisible(true);
			i = i + 1;
		}
	}
} // @jve:decl-index=0:visual-constraint="10,10"
